//
//  MainTableViewController.swift
//  weibo 666
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainTableViewController:  UITabBarController{
    
    
    @objc private func clickButton(){
        print("点击了")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewController()
        
        print(tabBar.addSubview)
        setMyButton()
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //将撰写放在最前面
        tabBar.bringSubviewToFront(MyButton)
    }
    
    //MARK : - 懒加载
    private lazy var MyButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}
////MARK:-设置主界面
extension MainTableViewController {
    ///设置按钮
    private func setMyButton()
    {
//        1添加按钮
        tabBar.addSubview(MyButton)
//        2调整按钮
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        MyButton.frame = CGRectInset(tabBar.bounds, 2 * w ,0)
//        添加按钮方法
        MyButton.addTarget(self, action: "clickButton", forControlEvents: .TouchUpInside)
    }

//    添加所有的控制器
    private func addChildViewController() {
        
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
//    添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
//        导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
}
    
